/*
 * Copyright (c) 2013, Charles Santos da Silva <silva.charlessantos@gmail.com> -
 * scharlessantos.org
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 */
package org.scharlessantos.atlas;

import org.scharlessantos.hermes.Hermes;

import java.lang.reflect.Field;

/**
 * Defines that Language who Atlas must translate and load translations
 * 
 * @author Charles Santos da Silva
 * 
 */
public enum Language {
	
	/**
	 * Translations for Portuguese from Brazil
	 */
	AF("af", "Afrikaans"),
	/**
	 * Translations for Arabic (Saudi Arabia)
	 */
	AR_SA("ar_SA", "(العربية (المملكة العربية السعودية"),
	/**
	 * Translations for Arabic (Egypt)
	 */
	AR_EG("ar_EG", "(العربية (مصر)"),
	/**
	 * Translations for Arabic (Argelia)
	 */
	AR_DZ("ar_DZ", "(العربية (الجزائر"),
	/**
	 * Translations for Portuguese from Brazil
	 */
	PT_BR("pt_BR", "Português (Brasil)"),
	/**
	 * Translations for Spanish from Spain
	 */
	ES_ES("es_ES", "Español (España)"),
	/**
	 * Translations for English from USA
	 */
	EN_US("en_US", "English (United States)"),
	/**
	 * Translations for English from United Kingdom
	 */
	EN_UK("en_US", "English (United Kingdom)"),
	/**
	 * Translations for Spanish from Colombia
	 */
	ES_CO("en_US", "English (United Kingdom)"),
	/**
	 * Translations for Spanish from Argentina
	 */
	ES_AR("en_US", "English (United Kingdom)"),
	
	;
	
	private String acronym;
	private String description;
	
	private Language(String acronym, String description) {
		this.acronym = acronym;
		this.description = description;
	}
	
	@Override
	public String toString() {
		return acronym;
	}
	
	/**
	 * Returns the acronym of language. See the table bellow to get an example: <br>
	 * <br>
	 * <table border="1">
	 * <tr>
	 * <th><b>Acronym</b></th>
	 * <th><b>Language (English)</b></th>
	 * <th><b>Language (native)</b></th>
	 * </tr>
	 * <tr>
	 * <td>pt_BR</td>
	 * <td>Portuguese (Brazil)</td>
	 * <td>Portugu&ecirc;s (Brasil)</td>
	 * </tr>
	 * <tr>
	 * <td>en_US</td>
	 * <td>English (United States)</td>
	 * <td>English (United States)</td>
	 * </tr>
	 * <tr>
	 * <td>en_UK</td>
	 * <td>English (United Kingdom)</td>
	 * <td>English (United Kingdom)</td>
	 * </tr>
	 * <tr>
	 * <td>es_ES</td>
	 * <td>Spanish (Spain)</td>
	 * <td>Espa&ntilde;ol (Espa&ntilde;a)</td>
	 * </tr>
	 * </table>
	 * 
	 * @return A String that contains the acronym for Language
	 */
	public String getAcronym() {
		return acronym;
	}
	
	/**
	 * Gets the description for language in native form. <br>
	 * 
	 * See {@link #getAcronym()}
	 * 
	 * @return String with native description
	 */
	public String getDescripton() {
		return description;
	}
	
	/**
	 * Returns the enum entry specified language acronym.<br>
	 * <br>
	 * The find is insensitive, so, for any case variation of same language acronym is able to
	 * returns the correct language
	 * 
	 * @param acronym (pt_BR, PT_BR, pt_BR)
	 * @return enum entry or null
	 */
	public static Language getByAcronym(String acronym) {
		for (Field f : Language.class.getFields())
			try {
				if (f.get(null) instanceof Language) {
					Language l = (Language) f.get(null);
					
					if (l.getAcronym().equalsIgnoreCase(acronym))
						return l;
				}
			} catch (IllegalArgumentException | IllegalAccessException e) {
				Hermes.error(e);
			}
		
		return null;
	}
}
